package com.vbez.sommelier.rawinfo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class WebPageReader {
	private static int processedDays = 0;

	public static BufferedReader getContent(String url) throws IOException {
		return new BufferedReader(new InputStreamReader(
				new URL(url).openStream(), "UTF-8"));
	}

	public List<RawItem> getItems(String url, int maxDays) {
		List<RawItem> allItems = new ArrayList<RawItem>();
		
		System.out.println("Read item(s) from URL: " + url);

		StringBuilder sb = new StringBuilder();

		boolean completed = false;
		while (!completed) {
			try {
				BufferedReader reader = getContent(url);

				String line = reader.readLine();
				while (line != null) {
					sb.append(line);
					line = reader.readLine();
				}
				completed = true;
			} catch (java.io.IOException ex) {
				System.out.println("Sleep...");
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.out.println("Next try");
			}
		}

		LJPageParser parser = new LJPageParser();
		List<RawItem> items = parser.parse(sb, 0);
		allItems.addAll(items);

		processedDays++;

		System.out.println("Processed days: " + processedDays);

		if (processedDays < maxDays) {
			if (items.size() > 0 && items.get(0).nextDayLink != null) {
				allItems.addAll(getItems(items.get(0).nextDayLink, maxDays));
			}
		}

		return allItems;
	}
}